package com.weixing.mall.provider.service.impl;

import cn.hutool.core.date.DateUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.weixing.mall.base.exception.BusinessException;
import com.weixing.mall.base.enums.IsEnableEnum;
import com.weixing.mall.core.support.auth.MemAuth;
import com.weixing.mall.core.utils.CommonUtil;
import com.weixing.mall.provider.api.model.enums.AgentLevelEnum;
import com.weixing.mall.provider.mapper.AgentApplyMapper;
import com.weixing.mall.provider.model.domain.Agent;
import com.weixing.mall.provider.model.domain.AgentApply;
import com.weixing.mall.provider.model.domain.AgentInfo;
import com.weixing.mall.provider.model.domain.AgentSettle;
import com.weixing.mall.provider.model.dto.ApplyForAgentBasicDto;
import com.weixing.mall.provider.model.dto.ApplyForAgentDto;
import com.weixing.mall.provider.model.dto.ApplyForAgentInfoDto;
import com.weixing.mall.provider.model.dto.ApplyForAgentSettleDto;
import com.weixing.mall.provider.service.IAgentApplyService;
import com.weixing.mall.provider.service.IAgentInfoService;
import com.weixing.mall.provider.service.IAgentService;
import com.weixing.mall.provider.service.IAgentSettleService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Date;

/**
 * <p>
 * 代理商模块-代理商申请表 服务实现类
 * </p>
 *
 * @author Small
 * @since 2019-11-18
 */
@Service
public class AgentApplyServiceImpl extends ServiceImpl<AgentApplyMapper, AgentApply> implements IAgentApplyService {

    @Autowired
    private IAgentService agentService;
    @Autowired
    private IAgentInfoService agentInfoService;
    @Autowired
    private IAgentSettleService agentSettleService;

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void apply(ApplyForAgentDto applyForAgentDto, MemAuth memAuth) {
        String agentSn = applyForAgentDto.getAgentSn();
        Agent agent = agentService.findByAgentSn(agentSn);
        if (null == agent) {
            throw new BusinessException("代理商优惠id不存在");
        } else {
            if (IsEnableEnum.DISABLE.getValue().equals(agent.getIsEnable()) ||
                    DateUtil.compare(new Date(), agent.getMaturityTime()) > 0
            ) {
                throw new BusinessException("该优惠id对应的代理商不可用，或已过期");
            }
            if (AgentLevelEnum.LEVEL_3.getValue().equals(agent.getAgentLevel())){
                throw  new BusinessException("该优惠id对应的代理商无推荐权限");
            }
        }

        ApplyForAgentBasicDto basic = applyForAgentDto.getBasic();
        Agent agentNew = new Agent();
        BeanUtils.copyProperties(basic, agentNew);
        agentNew.setPassword(CommonUtil.encryptPassword(applyForAgentDto.getPassword()));
        agentNew.setInviteId(applyForAgentDto.getAgentSn());
        agentService.save(agentNew);
        // 身份信息
        ApplyForAgentInfoDto info = applyForAgentDto.getInfo();
        AgentInfo agentInfo = new AgentInfo();
        BeanUtils.copyProperties(info, agentInfo);
        agentInfo.setAgentId(agentNew.getId());
        agentInfoService.save(agentInfo);
        // 结算信息
        ApplyForAgentSettleDto settle = applyForAgentDto.getSettle();
        AgentSettle agentSettle = new AgentSettle();
        BeanUtils.copyProperties(settle, agentSettle);
        agentSettle.setAgentId(agentNew.getId());
        agentSettleService.save(agentSettle);
    }
}
